script src="/lib/markdown/markdown.js"
script src="/lib/filedrop/filedrop-min.js"
script src="/lib/caret/jquery.caret.js"

javascript:
  $(function(){
    var updatePreview = function(){
      $("#preview").html(markdown.toHTML($("#text-input").val()));
    };
    $(document).ready(updatePreview());
    $("#text-input").on("keyup", function(event){
      updatePreview();
    });

    var options = {iframe: {url: '/upload'}};
    var zone = new FileDrop('text-input', options);
    zone.on.send.push(function(files){
      var unix_t = parseInt(new Date / 1000);
      for(var i=0; i<files.length;++i){
        name = unix_t + "." + files[i].name.split('.').pop().toLowerCase();
        files[i].name = name
        files[i].on.done.push(function(xhr,e){
          var target = $("#text-input");
          target.focus();
          var p = target.caret();
          var text = target.val();
          var input = text.substr(0,p) + "\n![" + name + "](/#{UPLOAD}/" + name + ")\n" + text.substr(p);
          target.val(input);
          updatePreview();
        });
        files[i].on.error.push(function(e, xhr){
          alert(xhr.response);
        });
        files[i].SendTo('/upload');
      }
    });
  });

== slim :header

/== slim :tab_edit

div.row-fluid
  div.span6
    form action="/edit" method="post" style="margin-top:15px;"
      div.row-fluid
        input#submit.btn.btn-primary.btn-large type="submit" value="作成/更新"
        input#cancel.btn.btn-large type="button" value="キャンセル" onclick="history.back();"
      div.row-fluid
        p ページ名
        input.input.span12 type="text" name="name" value="#{@name}" style="font-size: 24px;"
        p 本文

        textarea#text-input.span12 name="body" #{@body}
  div#preview.span6.well

